// frontend/js/process.js
new Vue({
    el: '#app',
    data: {
        order: null,
        loading: true,
        processNote: '',
        processing: false
    },
    mounted() {
        this.loadOrder();
    },
    methods: {
        async loadOrder() {
            this.loading = true;
            try {
                const urlParams = new URLSearchParams(window.location.search);
                const orderId = urlParams.get('orderId');
                
                if (!orderId) {
                    throw new Error('未找到订单ID');
                }
                
                console.log('正在加载订单:', orderId);
                this.order = await API.order.getById(orderId);
                console.log('订单详情:', this.order);
                
            } catch (error) {
                console.error('加载订单失败:', error);
                this.showError('加载订单失败: ' + (error.message || '未知错误'));
            } finally {
                this.loading = false;
            }
        },
        
        async processOrder(orderStatus) {
            if (this.processing) return;
            
            const statusText = orderStatus === 1 ? '完成' : '驳回';
            
            try {
                // 简单确认对话框
                const confirmed = confirm(`确定要${statusText}这个订单吗？`);
                if (!confirmed) return;
                
                this.processing = true;
                const loading = showLoading('处理中...');
                
                console.log('处理订单:', this.order.id, '状态:', orderStatus, '备注:', this.processNote);
                
                await API.order.process(this.order.id, {
                    orderStatus: orderStatus,
                    processNote: this.processNote || '无备注'
                });
                
                showSuccess(`订单已${statusText}`);
                
                // 更新页面状态
                this.order.orderStatus = orderStatus;
                this.order.processNote = this.processNote;
                
                // 3秒后返回首页
                setTimeout(() => {
                    window.location.href = 'index.html';
                }, 3000);
                
            } catch (error) {
                console.error('订单处理失败:', error);
                this.showError(`订单${statusText}失败: ${error.message || '未知错误'}`);
            } finally {
                this.processing = false;
                hideLoading(loading);
            }
        },
        
        showError(message) {
            if (typeof ELEMENT !== 'undefined' && ELEMENT.Message) {
                ELEMENT.Message.error(message);
            } else {
                alert(message);
            }
        },
        
        formatTime(timeString) {
            if (!timeString) return '未知时间';
            try {
                const date = new Date(timeString);
                return date.toLocaleString('zh-CN');
            } catch (error) {
                return timeString;
            }
        }
    }
});